var util = require('../../utils/util.js');
var api = require('../../config/api.js');

Page({
    data: {
        navList: [],
        categoryList: [],
        goodsCount: 0,
        nowId: 0,
        loading:0,
    },
    onLoad: function(options) {
        
    },
    onPullDownRefresh: function() {
        wx.showNavigationBarLoading()
        this.getCatalog();
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
    },
    getCatalog: function() {
        let that = this;
        util.request(api.CatesIndex).then(function (res) {
            if (res.code == 0) {
                that.setData({
                    navList: res.data,
                    nowId: res.data[0].id
                });
                util.request(api.CateTree, {cate_id: res.data[0].id}).then(function (ret) {
                    if (ret.code == 0) {
                        that.setData({
                            categoryList: ret.data,
                            loading: 1
                        })
                    }
                });
            }
        });
        util.request(api.GoodsCount).then(function (res) {
			if (res.code == 0) {
				that.setData({
				    goodsCount: res.data.count
				});
			}
        });
    },
    onShow: function() {
        this.getCatalog();
    },
    switchCate: function(e) {
        let that = this;
        let id = e.currentTarget.dataset.id;
        let nowId = this.data.nowId;
        if (id == nowId) {
            return false;
        } else {
            util.request(api.CateTree, {cate_id: id}).then(function (ret) {
                if (ret.code == 0) {
                    that.setData({
                        categoryList: ret.data,
                        nowId: id,
                        loading: 1
                    });
                }
            });
        }
    },
})